package io.renren.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.renren.mp.MybatisPlusGenerator;
import io.renren.mp.config.MybatisPlusConfig;
import io.renren.service.SysGeneratorService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.StringPool.COMMA;

/**
 * 代码生成器
 *
 * @author jerry chan
 * @email 670809626@qq.com
 * @date 2019年12月19日 下午9:12:58
 */
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {
    @Autowired
    private SysGeneratorService sysGeneratorService;
    @Autowired
    private MybatisPlusGenerator mybatisPlusGenerator;
    @Autowired
    private MybatisPlusConfig mybatisPlusConfig;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<Map<String, Object>> list = sysGeneratorService.queryList(query);
        int total = sysGeneratorService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 生成代码
     */
    @RequestMapping("/mybatisCode")
    public void mybatisCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] tableNames = new String[]{};
        String tables = request.getParameter("tables");
        tableNames = StringUtils.split(tables, COMMA);
        byte[] data = sysGeneratorService.generatorCode(tableNames);

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }

    /**
     * 生成代码 多表逗号分隔
     */
    @ResponseBody
    @RequestMapping("/mybatisPlusCode")
    public R mybatisPlusCode(@RequestParam String tables) throws IOException {
        String outputDir = mybatisPlusConfig.getOutputDir();
        if (StringUtils.isEmpty(outputDir)) {
            return R.error("未配置输出目录，请配置");
        }
        mybatisPlusGenerator.generate(tables, outputDir);
        return R.ok("成功生成代码, 所在目录为" + outputDir.replaceAll("\\\\", "/"));
    }

}
